package com.adu.api;

import java.io.Serializable;
import java.util.Map;

public class ApiResult<T> implements Serializable {

    public static final int CODE_SUCCESS = 0;
    public static final int CODE_FAIL = -1;
    public static final String MSG_SUCCESS = "success";
    public static final String MSG_FAIL = "fail";

    private int code = CODE_SUCCESS; // 返回码，0 正常
    private String msg = MSG_SUCCESS; // 返回信息
    private T data; // 返回数据
    private Map<String, Object> extraData;// 附加数据
    private long timestamp = System.currentTimeMillis();// 系统当前时间

    public static ApiResult success() {
        return success(null, null);
    }

    public static ApiResult success(Object data) {
        return success(data, null);
    }

    public static ApiResult success(Object data, Map<String, Object> extraData) {
        ApiResult apiResult = new ApiResult();
        apiResult.data = data;
        apiResult.extraData = extraData;
        return apiResult;
    }

    public static ApiResult failure() {
        return failure(CODE_FAIL, MSG_FAIL, null);
    }

    public static ApiResult failure(String msg) {
        return failure(CODE_FAIL, msg, null);
    }

    public static ApiResult failure(int code, String msg) {
        return failure(code, msg, null);
    }

    public static ApiResult failure(int code, String msg, Object data) {
        ApiResult result = new ApiResult();
        result.code = code;
        result.msg = msg;
        result.data = data;
        return result;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Map<String, Object> getExtraData() {
        return extraData;
    }

    public void setExtraData(Map<String, Object> extraData) {
        this.extraData = extraData;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }
}
